/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NuevoPOI.java
 *
 * Created on 26-abr-2010, 1:57:36
 */
package Vista;

import Modelo.Alertas;
import Modelo.Idiomas.LocaleManager;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;

/**
 *
 * @author Administrador
 */
public class NuevoPOI extends javax.swing.JDialog {

    private String nombre;
    private String tipo;
    private String descripcion;
    private DefaultComboBoxModel modelCB;
    private DefaultListModel modelList;
    private boolean ok;

    public NuevoPOI(javax.swing.JDialog parent, DefaultComboBoxModel modelCBPOI, DefaultListModel modelListPOI) {
        super(parent, true);
        modelCB = modelCBPOI;
        modelList = modelListPOI;
        ok = false;
        initComponents();
        cambioIdioma();
        centrarAplicacion();
        setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        LNombrePOI = new javax.swing.JLabel();
        LTipoPOI = new javax.swing.JLabel();
        LDescPOI = new javax.swing.JLabel();
        TXTNombre = new javax.swing.JTextField();
        CBTipo = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        TXTDescripcion = new javax.swing.JTextArea();
        BAceptar = new javax.swing.JButton();
        BCancelar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Información del POI");

        LNombrePOI.setText("Nombre");

        LTipoPOI.setText("Tipo");

        LDescPOI.setText("Descripcion");

        TXTNombre.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                TXTNombreKeyTyped(evt);
            }
        });

        CBTipo.setModel(modelCB);

        TXTDescripcion.setColumns(20);
        TXTDescripcion.setRows(5);
        TXTDescripcion.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                TXTDescripcionKeyTyped(evt);
            }
        });
        jScrollPane1.setViewportView(TXTDescripcion);

        BAceptar.setText("Aceptar");
        BAceptar.setEnabled(false);
        BAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BAceptarActionPerformed(evt);
            }
        });

        BCancelar.setText("Cancelar");
        BCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(LDescPOI, javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(LNombrePOI)
                            .addComponent(TXTNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(28, 28, 28)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(CBTipo, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(LTipoPOI))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(57, 57, 57)
                .addComponent(BAceptar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 62, Short.MAX_VALUE)
                .addComponent(BCancelar)
                .addGap(56, 56, 56))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(LNombrePOI)
                    .addComponent(LTipoPOI))
                .addGap(5, 5, 5)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(TXTNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(CBTipo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(LDescPOI)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(BAceptar)
                    .addComponent(BCancelar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void BCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BCancelarActionPerformed
        ok = false;
        dispose();
    }//GEN-LAST:event_BCancelarActionPerformed

    private void BAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BAceptarActionPerformed
        nombre = TXTNombre.getText();
        descripcion = TXTDescripcion.getText();
        tipo = (String) CBTipo.getSelectedItem();
        if (!nombre.equals("") && !descripcion.equals("") && !tipo.equals("")) {
            int i = 0;
            boolean encontrado = false;
            while (i < modelList.getSize() && !encontrado) {
                String name = modelList.getElementAt(i).toString();
                encontrado = name.equals(nombre);
                i++;
            }
            if (encontrado) {
                ok = false;
                Alertas.poiRepetido(null);
            }
            else {
                ok = true;
                dispose();
            }
        }
    }//GEN-LAST:event_BAceptarActionPerformed

    private void TXTNombreKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_TXTNombreKeyTyped
        if (!TXTNombre.getText().equals("") && !TXTDescripcion.getText().equals("") && !CBTipo.getSelectedItem().equals("")) {
            BAceptar.setEnabled(true);
        } else {
            BAceptar.setEnabled(false);
        }
    }//GEN-LAST:event_TXTNombreKeyTyped

    private void TXTDescripcionKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_TXTDescripcionKeyTyped
        if (!TXTNombre.getText().equals("") && !TXTDescripcion.getText().equals("") && !CBTipo.getSelectedItem().equals("")) {
            BAceptar.setEnabled(true);
        } else {
            BAceptar.setEnabled(false);
        }
    }//GEN-LAST:event_TXTDescripcionKeyTyped
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BAceptar;
    private javax.swing.JButton BCancelar;
    private javax.swing.JComboBox CBTipo;
    private javax.swing.JLabel LDescPOI;
    private javax.swing.JLabel LNombrePOI;
    private javax.swing.JLabel LTipoPOI;
    private javax.swing.JTextArea TXTDescripcion;
    private javax.swing.JTextField TXTNombre;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables

    private void centrarAplicacion() {
        int width = this.getWidth();
        int height = this.getHeight();
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (screen.width - width) / 2;
        int y = (screen.height - height) / 2;
        setBounds(x, y, width, height);
    }

    private void cambioIdioma() {
        this.setTitle(LocaleManager.getMessage("nuevoPOITitulo"));
        this.LNombrePOI.setText(LocaleManager.getMessage("nuevoPOINombre"));
        this.LDescPOI.setText(LocaleManager.getMessage("nuevoPOIDescripcion"));
        this.LTipoPOI.setText(LocaleManager.getMessage("nuevoPOITipo"));
    }

    /**
     * @return the nombre
     */
    public String getNombre() {
        return nombre;
    }

    /**
     * @return the tipo
     */
    public String getTipo() {
        return tipo;
    }

    /**
     * @return the descripcion
     */
    public String getDescripcion() {
        return descripcion;
    }

    /**
     * @return the ok
     */
    public boolean isOk() {
        return ok;
    }
}
